package controller;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.bean.Canbo;
import model.bean.Chucvu;
import model.bean.Coquan;
import model.dao.CanBoDAO;
import model.dao.ChucvuDAO;
import model.dao.CoquanDAO;

/**
 * Servlet implementation class CanBoServlet
 */
@WebServlet("/CanBoServlet")
public class CanBoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CanBoServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");

		

		CoquanDAO coquanDAO = new CoquanDAO();
		ChucvuDAO chucvuDAO = new ChucvuDAO();
		CanBoDAO canBoDAO = new CanBoDAO();

		if (request.getParameter("them") != null) {
			
			String idcv = request.getParameter("idcv");
			String idcq = request.getParameter("idcq");
			String date = request.getParameter("date");
			String sex = request.getParameter("sex");
			String tencb = request.getParameter("tencb");
			String dienthoai = request.getParameter("dienthoai");
			String email = request.getParameter("email");
			String diachicq = request.getParameter("diachicq");
			
			int idCanbo = canBoDAO.getCanboID();
			boolean success = canBoDAO.themCanBo(idCanbo, tencb, sex, date,
					diachicq, dienthoai, email, idcq, idcv);

			ArrayList<Coquan> dsCoquan = coquanDAO.dsCoquan();
			ArrayList<Chucvu> dsChucvu = chucvuDAO.dsChucvu();
			request.setAttribute("success", success ? "true" : "false");
			request.setAttribute("dsChucvu", dsChucvu);
			request.setAttribute("dsCoquan", dsCoquan);

			RequestDispatcher rd = request
					.getRequestDispatcher("form_canbo.jsp");
			rd.forward(request, response);
		}

		else if (request.getParameter("themmoi") != null) {

			ArrayList<Coquan> dsCoquan = coquanDAO.dsCoquan();
			ArrayList<Chucvu> dsChucvu = chucvuDAO.dsChucvu();

			request.setAttribute("dsChucvu", dsChucvu);
			request.setAttribute("dsCoquan", dsCoquan);

			RequestDispatcher rd = request
					.getRequestDispatcher("form_canbo.jsp");
			rd.forward(request, response);
		}

		else if (request.getParameter("dsCanbo") != null) {

			ArrayList<Canbo> dsCanbo = canBoDAO.dsCanbo();

			request.setAttribute("dsCanbo", dsCanbo);
			RequestDispatcher rd = request.getRequestDispatcher("ds_canbo.jsp");
			rd.forward(request, response);
		}

		else if (request.getParameter("new") != null) {
			ArrayList<Coquan> dsCoquan = coquanDAO.dsCoquan();
			ArrayList<Chucvu> dsChucvu = chucvuDAO.dsChucvu();

			request.setAttribute("dsChucvu", dsChucvu);
			request.setAttribute("dsCoquan", dsCoquan);

			RequestDispatcher rd = request
					.getRequestDispatcher("form_canbo.jsp");
			rd.forward(request, response);
		}
		else if(request.getParameter("boqua") != null) {
			ArrayList<Canbo> dsCanbo = canBoDAO.dsCanbo();

			request.setAttribute("dsCanbo", dsCanbo);
			RequestDispatcher rd = request.getRequestDispatcher("ds_canbo.jsp");
			rd.forward(request, response);
		}
		
		else if(request.getParameter("xoa") != null) {
			String id = request.getParameter("xoa");
			boolean xoa = canBoDAO.xoaCanbo(id);
			
			ArrayList<Canbo> dsCanbo = canBoDAO.dsCanbo();
			request.setAttribute("dsCanbo", dsCanbo);
			
			request.setAttribute("xoa",xoa ? "true" : "false");
			RequestDispatcher rd = request.getRequestDispatcher("ds_canbo.jsp");
			rd.forward(request, response);
		}
		
		else if(request.getParameter("sua") != null) {
			String id = request.getParameter("idcb");
			
			Canbo cb = canBoDAO.getCanbo(id);
					
			ArrayList<Coquan> dsCoquan = coquanDAO.dsCoquan();
			ArrayList<Chucvu> dsChucvu = chucvuDAO.dsChucvu();

			request.setAttribute("canbo", cb);
			request.setAttribute("dsChucvu", dsChucvu);
			request.setAttribute("dsCoquan", dsCoquan);

			RequestDispatcher rd = request
					.getRequestDispatcher("sua_canbo.jsp");
			rd.forward(request, response);
			
		}
		
		//cap nhat can bo
		else if(request.getParameter("idcb2") != null) {
			
			String idcb = request.getParameter("idcb2");
			String idcq = request.getParameter("idcq");
			String idcv = request.getParameter("idcv");
			String date = request.getParameter("date");
			String tencb = request.getParameter("tencb");
			String sex = request.getParameter("sex");
			String dienthoai = request.getParameter("dienthoai");
			String email = request.getParameter("email");
			String diachicq = request.getParameter("diachicq");
			
			Canbo cb = canBoDAO.getCanbo(idcb);
			
			ArrayList<Coquan> dsCoquan = coquanDAO.dsCoquan();
			ArrayList<Chucvu> dsChucvu = chucvuDAO.dsChucvu();

			request.setAttribute("canbo", cb);
			request.setAttribute("dsChucvu", dsChucvu);
			request.setAttribute("dsCoquan", dsCoquan);
			
			boolean sua = canBoDAO.capNhapCB(idcb, tencb, sex, date, diachicq, dienthoai, email, idcq, idcv);
			request.setAttribute("sua", sua ? "true":"false");
			

			RequestDispatcher rd = request
					.getRequestDispatcher("sua_canbo.jsp");
			rd.forward(request, response);
			
		}
		else {
			
			ArrayList<Canbo> dsCanbo = canBoDAO.dsCanbo();

			request.setAttribute("dsCanbo", dsCanbo);
			RequestDispatcher rd = request.getRequestDispatcher("ds_canbo.jsp");
			rd.forward(request, response);
		}

	}

}
